package com.itheima.service.impl;

import com.itheima.common.utils.DateUtils;
import com.itheima.config.RedisConfiguration;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * 根据预约id查询体检预约信息
     * @param id
     * @return
     */
    @Override
    public OrderReturn findById(Integer id) {
        Order order = orderMapper.findById(id);
        //将会员名查询出来并封装
        OrderReturn orderReturn = new OrderReturn();
        BeanUtils.copyProperties(order,orderReturn);
        orderReturn.setMember(orderMapper.findMemberById(order.getMemberId()));
        return orderReturn;
    }

    /**
     * 体检预约
     * @param orderSubmit
     */
    @Override
    public Integer submitOrder(OrderSubmit orderSubmit) throws Exception {
        //比较输入的验证码是否正确
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object validateCode = valueOperations.get(orderSubmit.getTelephone());
        //正确则添加预约
        if(orderSubmit.getValidateCode().equals(String.valueOf(validateCode))){
            //先去表里搜寻会员卡和会员名是否存在
            List<Member> memberList = memberMapper.getMemberByNameandIdCard(orderSubmit.getName(),orderSubmit.getIdCard());
            //不存在则先注册
            if(memberList == null || memberList.size() == 0){
                Member member = new Member();
                BeanUtils.copyProperties(orderSubmit,member);
                member.setSex(String.valueOf(orderSubmit.getSex()));
                member.setPhoneNumber(orderSubmit.getTelephone());
                member.setRegTime(DateUtils.parseString2Date(LocalDate.now().toString()));
                memberMapper.addMember(member,LocalDate.now());
                memberList.add(member);
            }
            //存在就添加预约信息
            //看看预约满没满
            OrderSetting orderSetting = orderSettingMapper.getRevesations(orderSubmit.getOrderDate());
            if(orderSetting.getReservations() == orderSetting.getNumber())
                throw new RuntimeException("预约已满");
            Integer memberId = memberList.get(0).getId();
            orderMapper.add(memberId,
                    orderSubmit.getOrderDate(),
                    Order.ORDERTYPE_WEIXIN,
                    Order.ORDERSTATUS_NO,
                    orderSubmit.getSetmealId());
            orderSettingMapper.addRevesations(orderSubmit.getOrderDate());
            return memberId;
        }
        //不正确则返回验证码不正确的结果
        else throw new RuntimeException("验证码不正确");
    }
}
